home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK1.toast / Development Kits (Disc 1) / ColorSync / Sample Code / CSDemo 2.1 / ShellSources / appPrint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-08  |  10.0 KB  |  319 lines  |  [TEXT/CWIE]

  1. #ifndef PIGS_SHELL_NOPRINT
  2.  
  3. #include <Types.h>
  4.  
  5. #include "appGlobals.h"
  6. #include "appPrint.h"
  7. #include "appMenus.h"
  8. #include "appErrors.h"
  9. #include "appAEvts.h"
  10.  
  11. #include "gxUtils.h"
  12. #include "gxPrintUtils.h"
  13. #include "qdPrintUtils.h"
  14.  
  15. #include "cursorUtils.h"
  16.  
  17. #include "win.h"
  18. #include "winTables.h"
  19.  
  20.  
  21. /**\
  22. |**| ==============================================================================
  23. |**| PRIVATE FUNCTION PROTOTYPES
  24. |**| ==============================================================================
  25. \**/
  26. void    AdjustMenusForPrinting    ( void ) ;
  27.  
  28.  
  29. /**\
  30. |**| ==============================================================================
  31. |**| PUBLIC FUNCTIONS
  32. |**| ==============================================================================
  33. \**/
  34.  
  35.  
  36. /*------------------------------------------------------------------------------*\
  37.     DoAppPageSetupCommand
  38.  *------------------------------------------------------------------------------*
  39. \*------------------------------------------------------------------------------*/
  40. void DoAppPageSetupCommand ( winHandle win ) 
  41. {
  42.     OSErr            err ;
  43.     FSSpec            spec = {0,0,"\p"} ;
  44.  
  45.     if ( win==nil) return ;
  46.     err = DoAppGetPrintStrutures( win ) ;    // make sure that we have the needed print structures
  47.     WarnIfErr( err ) ;
  48.     if ( err ) return ;
  49.     
  50.     HiliteMenu(0) ;                            // Unhighlight whatever MenuSelect or MenuKey hilited
  51.     AdjustMenusForPrinting() ;
  52.     
  53.     if( QDGX_available()!=noErr )            // if GX is not installed then do the classic page setup dialog
  54.         err = QDStyleDlog( GetWinPrintRec(win) ) ;
  55.     else                                    // handle the GX case page setup dialog
  56.         err = GXStyleDlog( GetWinGXJob(win) ) ;
  57.  
  58.     spec = GetWinFSSpec( win ) ;
  59.     if (spec.name[0])                        // if the win has an associated file
  60.     {
  61.         if( QDGX_available()!=noErr )
  62.             err = SaveResourceTHPrint( GetWinPrintRec(win), &spec ) ;
  63.         else
  64.             err = SaveResourceGXJob( GetWinGXJob(win), &spec ) ;
  65.     }
  66. }
  67.  
  68.  
  69. /*------------------------------------------------------------------------------*\
  70.     DoAppPrintCommand
  71.  *------------------------------------------------------------------------------*
  72. \*------------------------------------------------------------------------------*/
  73. void DoAppPrintCommand ( winHandle win ) 
  74. {
  75.     OSErr            err ;
  76.     FSSpec            spec = {0,0,"\p"} ;
  77.     
  78.     if ( win==nil) return ;
  79.     err = DoAppGetPrintStrutures( win ) ;    // make sure that we have the needed print structures
  80.     WarnIfErr( err ) ;
  81.     if ( err ) return ;
  82.     
  83.     HiliteMenu(0) ;                            // Unhighlight whatever MenuSelect or MenuKey hilited
  84.     AdjustMenusForPrinting() ;
  85.  
  86.     if( QDGX_available()!=noErr )            // if GX is not installed then do the classic page setup dialog
  87.         err = QDJobDlog( GetWinPrintRec(win) ) ; 
  88.     else                                    // handle the gx page setup dialog
  89.         err = GXJobDlog( GetWinGXJob(win) ) ;
  90.     if (err) return ;
  91.  
  92.     spec = GetWinFSSpec( win ) ;
  93.     if (spec.name[0])                        // if the win has an associated file
  94.     {
  95.         if( QDGX_available()!=noErr )
  96.             err = SaveResourceTHPrint( GetWinPrintRec(win), &spec ) ;
  97.         else
  98.             err = SaveResourceGXJob( GetWinGXJob(win), &spec ) ;
  99.     }
  100.  
  101.     DoAppPrintOneCommand( win ) ;
  102. }
  103.  
  104.  
  105. /*------------------------------------------------------------------------------*\
  106.     DoAppPrintOneCommand
  107.  *------------------------------------------------------------------------------*
  108. \*------------------------------------------------------------------------------*/
  109. void DoAppPrintOneCommand ( winHandle win ) 
  110. {
  111.     OSErr            err ;
  112.     FSSpec            spec = {0,0,"\p"} ;
  113.     
  114.     if ( win == nil) return ;
  115.     err = DoAppGetPrintStrutures( win ) ;    // make sure that we have the needed print structures
  116.     WarnIfErr( err ) ;
  117.     if ( err ) return ;
  118.     
  119.     // we can just issue a print command, because we have a valid print record for the 
  120.     // document.  This is in the dPrintRec record of the document record, we created
  121.     // it (or read it in from the document's file) at the time we created the window.
  122.     // for more information on this look at the routines SetupPrintHdl() and LoadPHdl()
  123.  
  124.     spec = GetWinFSSpec( win ) ;
  125.     
  126.     if (spec.name[0])                        // if the win has an associated file
  127.         SendPDOC( &spec ) ;                    // send print apple event
  128.     else
  129.         DoAppPrintLoop( win ) ;
  130. }
  131.  
  132.  
  133. /*------------------------------------------------------------------------------*\
  134.     DoAppGetPrintStrutures
  135.  *------------------------------------------------------------------------------*
  136. \*------------------------------------------------------------------------------*/
  137. OSErr DoAppGetPrintStrutures ( winHandle win )
  138. {
  139.     FSSpec             spec ;
  140.     THPrint            theTHPrint = nil ;
  141.     gxJob            theJob = nil ;
  142.     OSErr            err ;
  143.     
  144.     spec = GetWinFSSpec( win ) ;
  145.     
  146.     if ((err=GXPrinting_available()) == noErr )                // if GX printing is possible
  147.     {
  148.         if ( GetWinGXJob(win) ) return noErr ;                 // we already have it so return 
  149.         if (spec.name[0])                                    // if the win has an associated file
  150.             err = LoadResourceGXJob( &theJob, &spec ) ;        // get job from file
  151.         if (theJob==nil)                                    // if didnt get it
  152.             err = GetDefaultGXJob( &theJob ) ;                // get default
  153.         SetWinGXJob( win, theJob ) ;
  154.     }
  155.     else if ( (err=QDPrinting_available()) == noErr )        // else if old printing is possible
  156.     {
  157.         if ( GetWinPrintRec(win) ) return noErr ;             // we already have it so return 
  158.         if (spec.name[0])                                    // if the win has an associated file
  159.             err = LoadResourceTHPrint( &theTHPrint,&spec) ;    // get job from file
  160.         if (theTHPrint==nil)                                // if didnt get it
  161.             err = GetDefaultTHPrint( &theTHPrint ) ;        // get default
  162.         SetWinPrintRec( win, theTHPrint ) ;
  163.     }
  164.     return err ;                                            // return above error 
  165. }
  166.  
  167.  
  168. /*------------------------------------------------------------------------------*\
  169.     DoAppGetPageSize
  170.  *------------------------------------------------------------------------------*
  171. \*------------------------------------------------------------------------------*/
  172. OSErr DoAppGetPageSize ( winHandle win, Rect *pageRect )
  173. {
  174.     THPrint            theTHPrint = nil ;
  175.     gxJob            theJob = nil ;
  176.     OSErr            err ;
  177.     
  178.     err = DoAppGetPrintStrutures( win ) ;    // make sure that we have the needed print structures
  179.     WarnIfErr( err ) ;
  180.     if ( err ) return err ;
  181.     
  182.     if ((err=GXPrinting_available()) == noErr )                // if GX printing is possible
  183.     {
  184.         theJob = GetWinGXJob( win ) ;
  185.         err = GXGetPageSize( theJob, pageRect ) ;
  186.     }
  187.     else if ( (err=QDPrinting_available()) == noErr )        // else if old printing is possible
  188.     {
  189.         theTHPrint = GetWinPrintRec(win) ;
  190.         err = QDGetPageSize( theTHPrint, pageRect ) ;
  191.     }
  192.     return err ;                                            // return above error 
  193. }
  194.  
  195.  
  196. /*------------------------------------------------------------------------------*\
  197.     DoAppPrintLoop
  198.  *------------------------------------------------------------------------------*
  199.         This is a basic GX print loop for QuickDraw-based applications
  200.         For simplicity, we don't implement a facility for the user to select
  201.         different formats for each page.  In another app (a word processor
  202.         for example) we may want to do this, in which case the jobFormat field
  203.         of the document record could be implemented as an array with one 
  204.         slot in the array for each page in the win, or as a list.  We wanted
  205.         to keep this simple...
  206. \*------------------------------------------------------------------------------*/
  207. OSErr DoAppPrintLoop ( winHandle win )
  208. {
  209.     short            pageCount ;
  210.     long            firstPage, lastPage;
  211.     GrafPtr            savedPort ;
  212.     OSErr            err ;
  213.     
  214.     err = DoAppGetPrintStrutures( win ) ;    // make sure that we have the needed print structures
  215.     WarnIfErr( err ) ;
  216.     if ( err ) return err ;
  217.  
  218.     err = CallWinPageCountProc ( win, &pageCount ) ;
  219.     firstPage = 1; lastPage = pageCount ;
  220.     WarnIfErr( err ) ;
  221.     if ( err ) return err;
  222.     
  223.     SetUpCursors( kWatchACursor ) ;
  224.     GetPort( &savedPort ) ;
  225.     
  226.  
  227.     if ( (err=GXPrinting_available()) == noErr )        // if GX printing is possible
  228.     {
  229.         GXPrintLoopParamsRec    params ;
  230.  
  231.         params.theJob = GetWinGXJob(win) ;
  232.         params.window = GetWinWindow(win) ;
  233.         params.firstPage = firstPage ;
  234.         params.lastPage = lastPage ;
  235.             
  236.         err = GXPrintLoopBegin( ¶ms ) ;
  237.         if (!err)
  238.         {
  239.             for (    params.currentPage = params.firstPage;
  240.                     params.currentPage <= params.lastPage && !err ;
  241.                     params.currentPage ++ )
  242.             {
  243.                 if (!err) err = GXPrintLoopPageBefore( ¶ms ) ;
  244.                 if (!err) err = CallWinPagePrintProc( win, params.printingPort, params.currentPage ) ;
  245.                 if (!err) err = GXPrintLoopPageAfter( ¶ms ) ;
  246.             }
  247.             err = GXPrintLoopEnd( ¶ms ) ;
  248.         }
  249.     }
  250.     else if ( (err=QDPrinting_available()) == noErr )    // else if old printing is possible
  251.     {
  252.         QDPrintLoopParamsRec    params ;
  253.  
  254.         params.hPrint = GetWinPrintRec(win) ;
  255.         params.window = GetWinWindow(win) ;
  256.         params.firstPage = firstPage ;
  257.         params.lastPage = lastPage ;
  258.             
  259.         err = QDPrintLoopBegin( ¶ms ) ;
  260.         if (!err)
  261.         {
  262.             for (    params.currentPage = params.firstPage;
  263.                     params.currentPage <= params.lastPage && !err ;
  264.                     params.currentPage ++ )
  265.             {
  266.                 if (!err) err = QDPrintLoopPageBefore( ¶ms ) ;
  267.                 if (!err) err = CallWinPagePrintProc( win, params.printingPort, params.currentPage ) ;
  268.                 if (!err) err = QDPrintLoopPageAfter( ¶ms ) ;
  269.             }
  270.             err = QDPrintLoopEnd( ¶ms ) ;
  271.         }
  272.     }
  273.  
  274.  
  275.     SetPort( savedPort ) ;
  276.     TearDownCursors( kArrowCursor ) ;
  277.     return err ;                                        // return above error 
  278. }
  279.  
  280.  
  281. /**\
  282. |**| ==============================================================================
  283. |**| PRIVATE FUNCTIONS
  284. |**| ==============================================================================
  285. \**/
  286.  
  287.  
  288. /*------------------------------------------------------------------------------*\
  289.     AdjustMenusForPrinting
  290.  *------------------------------------------------------------------------------*
  291.         This routine enables the Edit menu items so that print dialogs can
  292.         do cut, copy, and paste.
  293.         This routine is called by the menu handling code of any windows that 
  294.         support Page Setup or Print commands.
  295. \*------------------------------------------------------------------------------*/
  296. static void AdjustMenusForPrinting ( void ) 
  297. {
  298.     MenuHandle    theMenu ;
  299.  
  300.     // if we are printing, disable everything except the edit menu
  301.  
  302.     dimAllMenus() ;
  303.         
  304.     // do the method menu
  305.     theMenu = GetMenuHandle ( mEdit ) ;
  306.         EnableItem ( theMenu, kWholeMenu ) ;
  307.         EnableItem ( theMenu, iUndo ) ;
  308.         EnableItem ( theMenu, iCut ) ;
  309.         EnableItem ( theMenu, iCopy ) ;
  310.         EnableItem ( theMenu, iPaste ) ;
  311.         EnableItem ( theMenu, iClear ) ;
  312.  
  313.     DrawMenuBar() ;
  314. }
  315.  
  316. #endif
  317.  
  318.  
  319.